//
//  CommonTool.m
//  HelloFriend
//
//  Created by going-HNL on 16/1/8.
//  Copyright © 2016年 going-HNL. All rights reserved.
//

#import "CommonTool.h"

@implementation CommonTool
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

+(void)hud:(NSString *)str time:(float)time{
    
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:result.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = str;
    hud.margin = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:time];
}

+(void)setNav:(UIViewController *)vc{
    vc.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    [vc.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [vc.navigationItem.backBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:FONT_BOLD(18),NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
}
@end
